<?php
/**
 * Created by PhpStorm.
 * User: zq
 * Date: 2017/3/5
 * Time: 23:43
 */

namespace Home\Model;


use Common\Model\BaseModel;

class CartModel extends BaseModel
{
    public function cartAdd(){
        //$cartSession=[];
        $cartSession=$_SESSION['cart'];
        $isbuys=[];
        //dump(I('post.checkbox') );
        foreach(I('post.checkbox') as $k=>$v){
            $isbuys[]=$k;
        }
        //dump($isbuys);
        foreach($cartSession as $k=>$v){
            foreach($cartSession['goods'] as $kk=>$vv){
                if(in_array($kk,$isbuys)){
                    $cartSession['goods'][$kk]['isbuy']=1;
                }else{
                    $cartSession['goods'][$kk]['isbuy']=2;
                }
                //dump($vv);
            }
        }

        //dump($cartSession);die;

        foreach($cartSession['goods'] as $k=>$v){
            $cartData['quantity']=$v['num'];
            $cartData['totalprice']=$v['total'];
            $gid=$v['id'];
            $glid=[];
            foreach($v['options'] as $kk=>$vv){
                $glid[]=m('goods_attr')->where("gavalue='{$vv}' and gid={$gid}")->getField('gaid');
            }
            $glid=implode('|',$glid);
            $cartData['glid']=m('goodslist')->where("combine='{$glid}'and gid={$gid} ")->getField('glid');
            $cartData['number']=m('goodslist')->where("glid={$cartData['glid']}")->getField('number');
            $cartData['gspec']=implode('|',$v['options']);
            $cartData['gid']=$gid;
            $cartData['isbuy']=$v['isbuy'];
            $cartData['time']=time();
            $cartData['uid']=session('user.uid');
            $this->store($cartData);
            //dump($cartData);
            //dump($v['id']);
        }
        //生成订单号
        $onumber = session('user.uid').time();
        session('onumber.onum',$onumber);
        return true;
    }

}